package com.agileboot.admin.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 请求映射打印工具
 * 用于打印所有注册的Controller请求映射路径
 */
//@Component
public class RequestMappingPrinter implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(RequestMappingPrinter.class);

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        logger.info("===================开始打印所有注册的Controller请求映射路径====================");
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

        // 使用AtomicInteger作为计数器，解决Lambda中使用非final变量的问题
        AtomicInteger totalControllers = new AtomicInteger(0);
        AtomicInteger attendanceControllers = new AtomicInteger(0);
        AtomicInteger systemControllers = new AtomicInteger(0);
        AtomicInteger otherControllers = new AtomicInteger(0);

        // 收集考勤模块的控制器信息
        List<String> attendanceEndpoints = new ArrayList<>();

        map.forEach((info, method) -> {
            String controllerName = method.getBeanType().getSimpleName();
            String methodName = method.getMethod().getName();
            String controllerPackage = method.getBeanType().getPackage().getName();
            String mappingInfo = info.toString();

            totalControllers.incrementAndGet();

            // 记录各类控制器
            if (controllerPackage.contains("attendance")) {
                attendanceControllers.incrementAndGet();
                attendanceEndpoints.add(String.format("控制器: %s - 方法: %s - 映射路径: %s",
                    controllerName, methodName, mappingInfo));
            } else if (controllerPackage.contains("system")) {
                systemControllers.incrementAndGet();
            } else {
                otherControllers.incrementAndGet();
            }

            logger.info("控制器: {} - 方法: {} - 包路径: {} - 映射路径: {}",
                controllerName, methodName, controllerPackage, mappingInfo);
        });

        // 打印统计信息
        logger.info("===================控制器统计信息====================");
        logger.info("总控制器数量: {}", totalControllers.get());
        logger.info("考勤模块控制器数量: {}", attendanceControllers.get());
        logger.info("系统模块控制器数量: {}", systemControllers.get());
        logger.info("其他模块控制器数量: {}", otherControllers.get());

        // 特别打印考勤模块的端点
        if (attendanceControllers.get() > 0) {
            logger.info("===================考勤模块端点列表====================");
            attendanceEndpoints.forEach(logger::info);
        } else {
            logger.warn("警告：未检测到考勤模块(attendance)下的控制器，请检查组件扫描配置！");
        }

        logger.info("===================结束打印所有注册的Controller请求映射路径====================");
    }
}
